<?php
declare (strict_types = 1);

namespace app\api\controller;
use app\BaseController;

use think\facade\Db;
use app\api\model\MenuModel;
use app\common\model\AdminModel;
use app\common\model\DingdingModel;     //钉钉消息通知

use think\captcha\facade\Captcha;
use think\facade\Request;
use think\facade\Session;

    /**
	 * Created by Ailcc.com
     * 2021-11-14 12:00:00 沛霖主页
     * 
	 */
class Login extends BaseController
{
    public function login(){

        $username = input('username');
        $password = input('password');
        $vercode = input('vercode');
        $Dingip = get_client_ip();
        $auth = new DingdingModel();

        if( !Captcha::check($vercode)) {
            $Dingmsg = '后台登录，账户'.$username.'验证码错误，登录失败，登录IP：'.$Dingip.'';
            $str = $auth->dingding_up($Dingmsg);
            return json(['code'=>1001, 'msg'=>'验证码错误']);
        }

        $userinfo = AdminModel::where('username',$username)->find();
        if(empty($userinfo)){
            return json(['code'=>1001, 'msg'=>'用户不存在']);
        }

        if(!password_verify( $password, $userinfo->password)){
            $Dingmsg = '后台登录，账户'.$username.'密码错误，登录失败，登录IP：'.$Dingip.'';
            $str = $auth->dingding_up($Dingmsg);
            return json(['code'=>1001, 'msg'=>'密码错误']);
        }

        Session::set('admin_id', $userinfo->id);
        Session::set('adminInfo', $userinfo);

        $access_token = 'my_access_token';
        $data = [
            'access_token' => $access_token
        ];
        $Dingmsg = '后台登录，账户'.$username.'登录成功，登录IP：'.$Dingip.'';
        $str = $auth->dingding_up($Dingmsg);
        return json(['code'=>0, 'msg'=>'登入成功', 'data'=>$data]);
        
    }

    /**
	 * Created by Ailcc.com
     * 2021-11-14 12:00:00 沛霖主页
     * 判断是否还是登录状态
	 */
    public function checkLogin(){
        $admin_id = Session::get('admin_id');
        if(empty($admin_id)){
            return json(['code'=>111, 'msg'=>'登录失效']);
        }else{
            return json(['code'=>0, 'msg'=>'登录状态']);
        }
    }

    public function logout(){
        Session::delete('admin_id');
        Session::delete('adminInfo');
        return json(['code'=>0, 'msg'=>'退出成功']);
    }

    public function verify()
    {
        return Captcha::create();
    }

    //特殊通道，可以保存当做无密码登录，没办法，懒得输入密码所以只能写这么个函数
// 	public function channel(){
// 		$userinfo = AdminModel::where('username','admin')->find();
// 		if(empty($userinfo)){
// 			return json(['code'=>1001, 'msg'=>'用户不存在']);
// 		}

// 		Session::set('admin_id', $userinfo->id);
// 		Session::set('adminInfo', $userinfo);

// 		$access_token = 'my_access_token';
// 		$data = [
// 			'access_token' => $access_token
// 		];
// 		$adminUrl =  Request::scheme().'://'.Request::host().'/admin';
// 		return redirect($adminUrl);
// 	}


    // public function test(){
    //     return 1;
    //     $list = AdminModel::select();
    //     print_r($list);
    //     exit;
    //     $p = '123456';
    //     // return $ps = mypassword($p);

    //     $ps = '$2y$10$ycAv./KeM5OoZ.tfm6NDYev6q2aa1c6yk.l3JroWIUFD8XZd6nVCq';
    //     $verify = password_verify('123456', $ps);
    //     if($verify){
    //         echo 1;
    //     }else{
    //         echo 0;
    //     }
    // }
}
